package com.jogger.newprint.ui;

import android.content.Intent;
import android.view.KeyEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.TextView;

import com.jogger.newprint.R;
import com.jogger.newprint.base.BaseActivity;
import com.jogger.newprint.utils.ToastUtil;

import butterknife.BindView;
import butterknife.OnClick;

public class RePrintActivity extends BaseActivity implements TextView.OnEditorActionListener {
    @BindView(R.id.tv_print_name)
    TextView tv_print_name;
    @BindView(R.id.et_area)
    EditText et_area;
    @BindView(R.id.et_grn)
    EditText et_grn;
    @BindView(R.id.et_pn)
    EditText et_pn;
    @BindView(R.id.et_qty)
    EditText et_qty;
    @BindView(R.id.et_lot)
    EditText et_lot;
    @BindView(R.id.et_date)
    EditText et_date;
    @BindView(R.id.et_vat)
    EditText et_vat;
    @BindView(R.id.et_factory)
    EditText et_factory;
    @BindView(R.id.et_desc)
    EditText et_desc;
    private int enterCount;//按下回车的次数

    @Override
    public int getLayoutId() {
        return R.layout.activity_re_print;
    }

    @Override
    protected void init() {
        super.init();
        et_area.setOnEditorActionListener(this);
    }

    @OnClick({R.id.ibtn_set})
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ibtn_set:
                Intent intent = new Intent(this, ConnectActivity.class);
                startActivityForResult(intent, 0);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case 0:
                if (resultCode == RESULT_OK) {
                    String printName = data.getStringExtra("printName");
                    String printMacAddress = data.getStringExtra("printMacAddress");
                    tv_print_name.setText(printName);
                }
                break;
        }
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        //当actionId == XX_SEND 或者 XX_DONE时都触发
        //或者event.getKeyCode == ENTER 且 event.getAction == ACTION_DOWN时也触发
        //注意，这是一定要判断event != null。因为在某些输入法上会返回null。
        if (actionId == EditorInfo.IME_ACTION_SEND
                || actionId == EditorInfo.IME_ACTION_DONE
                || (event != null && KeyEvent.KEYCODE_ENTER == event.getKeyCode() && KeyEvent.ACTION_DOWN == event.getAction())) {
            enterCount++;
            if (enterCount > 7) {
                //处理事件
                String text = v.getText().toString();
                try {
                    String[] split = text.split("\\n");
                    et_grn.setText(split[0]);
                    et_pn.setText(split[1]);
                    et_qty.setText(split[2]);
                    et_lot.setText(split[3]);
                    et_date.setText(split[4]);
                    et_vat.setText(split[5]);
                    et_factory.setText(split[6]);
                    et_desc.setText(split[7]);

                } catch (Exception e) {
                    e.printStackTrace();
                    ToastUtil.showToast(getApplicationContext(), "解析失败");
                }
            }
        }
        return false;
    }
}
